import 'package:fish_redux/fish_redux.dart';
import 'package:flutter/material.dart';

import 'action.dart';
import 'state.dart';

Widget buildView(
    DeviceLogDetailState state, Dispatch dispatch, ViewService viewService) {
  return Scaffold(
    appBar: AppBar(
      title: Text(state.time),
    ),
    body: GestureDetector(
      onTap: () {
        //隐藏输入法
        FocusScope.of(viewService.context).requestFocus(new FocusNode());
      },
      child: Column(
        children: <Widget>[
          Container(
            color: Colors.grey[300],
            padding: EdgeInsets.all(8),
            // height: 40,
            child: Stack(
              alignment: AlignmentDirectional.topEnd,
              children: <Widget>[
                TextField(
                  // controller: state.mController,
                  onChanged: (msg) {
                    dispatch(DeviceLogDetailActionCreator.filterMsg(msg));
                  },
                  decoration: InputDecoration(
                    hintText: '请输入关键字',
                    labelText: '关键字搜索',
                  ),
                  maxLines: 1,
                  maxLength: 20,
                  style: TextStyle(fontSize: 12),
                  // keyboardType: TextInputType.multiline,
                ),
                // ),
                Offstage(
                  offstage: state.mController.text.length == 0,
                  child: IconButton(
                    icon: Icon(Icons.clear),
                    onPressed: () {
                      dispatch(DeviceLogDetailActionCreator.clearFilterMsg());
                    },
                  ),
                ),
              ],
            ),
          ),
          Expanded(
            child: ListView(
              children: _createMsgRows(state, dispatch, viewService),
            ),
          ),
        ],
      ),
    ),
    floatingActionButton: FloatingActionButton(
      child: Icon(Icons.refresh),
      onPressed: () {
        dispatch(DeviceLogDetailActionCreator.reload());
      },
    ),
  );
}

List<Widget> _createMsgRows(
    DeviceLogDetailState state, Dispatch dispatch, ViewService viewService) {
  var list = List<Widget>();
  state.msgFliterList.forEach((line) {
    list.add(Text(line, style: TextStyle(fontSize: 8)));
  });
  return list;
}
